#!/usr/bin/env python
# coding: utf-8

DEFAULT_ATTR='node'

class Dyn_tree(dict):
    def  __init__(self, name,attr=DEFAULT_ATTR):

        self.update({"name":name})
        self.update({"attr":attr})
        return None                                ####初始化，只能返回None
    #添加分支
    def add_children(self,children):
        try:
            self['children'].append(children)
            #self.update({"children":self['children'].append(children)})
            return self
        except:
            self.update({"children":[children]})
    #移除分支
    def remove_child(self,child):
        try:
            self['children'].remove(child)
            return self
        except:
            try:
                for i in self['children']:
                    i.remove_child(child)
                return self
            except:
                pass
                return self
        

#do something while traversal
#通用遍历函数，可以遍历的同时，根据传入的函数做某些动作
  
     
    def dosomething(self,func,*args):
        try:
            try:
                func(self,*args)

            except:
                
                pass
            [x.dosomething(func,*args) for x in self['children']]
        except:
            pass 
        return args


##outside function for Json_class object
#遍历时使用的外部函数

